package cn.com.tianpeidong.restaurantadmin.project.admin.chart.rest;

import cn.com.tianpeidong.restaurantadmin.project.admin.chart.mapper.ChartMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author tianpeidong
 * @version 创建时间: 19-5-21 下午8:23
 */
@RestController
@RequestMapping("/chart")
public class ChartController {

    @Autowired
    private ChartMapper chartMapper;

    @PostMapping
    public JSONObject getTotalInfo(@RequestBody ChartRequest chartRequest) {
        JSONObject result = new JSONObject();
        // total
        JSONObject totalInfo = chartMapper.getTotalInfo(chartRequest.getDateValue().get(0), chartRequest.getDateValue().get(1));
        // top10 income
        JSONObject top10IncomeData = new JSONObject();
        List<Top10s> top10Income = chartMapper.getTop10Income(chartRequest.getDateValue().get(0), chartRequest.getDateValue().get(1));
        List<String> incomeLegendData = top10Income.stream().map(t -> t.getDishesName()).collect(Collectors.toList());
        JSONArray incomeSeriesData = new JSONArray();
        for (Top10s top10s : top10Income) {
            JSONObject item = new JSONObject();
            item.put("name", top10s.getDishesName());
            item.put("value", top10s.getTotalIncome());
            incomeSeriesData.add(item);
        }
        top10IncomeData.put("legend", incomeLegendData);
        top10IncomeData.put("series", incomeSeriesData);

        // top10 pop
        List<Top10s> top10Pop = chartMapper.getTop10Pop(chartRequest.getDateValue().get(0), chartRequest.getDateValue().get(1));
        JSONObject top10PopData = new JSONObject();
        List<String> legendData = top10Pop.stream().map(t -> t.getDishesName()).collect(Collectors.toList());
        JSONArray seriesData = new JSONArray();
        for (Top10s top10s : top10Pop) {
            JSONObject item = new JSONObject();
            item.put("name", top10s.getDishesName());
            item.put("value", top10s.getTotalCount());
            seriesData.add(item);
        }
        top10PopData.put("legend", legendData);
        top10PopData.put("series", seriesData);

        // top10 un pop
        JSONObject top10UnPopData = new JSONObject();
        List<Top10s> top10UnPop = chartMapper.getTop10UnPop(chartRequest.getDateValue().get(0), chartRequest.getDateValue().get(1));
        List<String> xAxisData = top10UnPop.stream().map(t -> t.getDishesName()).collect(Collectors.toList());
        JSONArray unPopseriesData = new JSONArray();
        for (Top10s top10s : top10UnPop) {
            JSONObject item = new JSONObject();
            item.put("name", top10s.getDishesName());
            item.put("value", top10s.getTotalCount());
            unPopseriesData.add(item);
        }
        top10UnPopData.put("legend", xAxisData);
        top10UnPopData.put("series", unPopseriesData);

        // food type
        JSONObject foodType = chartMapper.getFoodType(chartRequest.getTid(), chartRequest.getDateValue().get(0), chartRequest.getDateValue().get(1));

        result.put("totalInfo", totalInfo);
        result.put("top10Income", top10IncomeData);
        result.put("top10Pop", top10PopData);
        result.put("top10UnPop", top10UnPopData);
        result.put("foodType", foodType);
        return result;
    }

}
